//
//  LXMovieGroupTitleView.m
//  cat
//
//  Created by imera on 16/1/14.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXMovieGroupTitleView.h"

@interface LXMovieGroupTitleView()
/*** 分类视频标题 */
@property (nonatomic, copy) NSString *title;
/*** 图标UI */
@property (nonatomic, weak) UIImageView *imageView;
/*** 标题UI */
@property (nonatomic, weak) UILabel *titleLabel;
/*** 图标字典 */
@property (nonatomic, strong) NSDictionary *iconDict;
@end

@implementation LXMovieGroupTitleView

+ (instancetype)initWithTitle:(NSString *)title
{
    return [[self alloc]initWithTitle:title];
}

- (instancetype)initWithTitle:(NSString *)title
{
    self.title = title;

    return [self init];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        UIImageView *imageView = [[UIImageView alloc]init];
        self.imageView = imageView;
        [self addSubview:imageView];
        
        UILabel *titleLabel = [[UILabel alloc]init];
        titleLabel.font = LXMovieGroupTitleViewTitleFont;
        self.titleLabel = titleLabel;
        [self addSubview:titleLabel];
        
        //获取本地plist文件
        NSString *path = [[NSBundle mainBundle]pathForResource:@"icon" ofType:@"plist"];
        NSDictionary *iconDict = [NSDictionary dictionaryWithContentsOfFile:path];
        self.iconDict = iconDict;
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    UIImageView *imageView = self.imageView;
    imageView.image = [UIImage imageNamed:self.iconDict[self.title]];
    CGFloat imageViewX = LXMovieGroupTitleViewMargin;
    CGFloat imageViewY = LXMovieGroupTitleViewMargin;
    CGFloat imageViewW = 20;
    CGFloat imageViewH = 20;
    imageView.frame = CGRectMake(imageViewX, imageViewY, imageViewW, imageViewH);
    
    UILabel *titleLabel = self.titleLabel;
    titleLabel.text = self.title;
    CGSize titleSize = [titleLabel.text sizeWithAttributes:@{NSFontAttributeName:LXMovieGroupTitleViewTitleFont}];
    CGFloat titleLabelX = CGRectGetMaxX(imageView.frame) + LXMovieGroupTitleViewMargin;
    CGFloat titleLabelY = imageViewY;
    CGFloat titleLabelW = titleSize.width;
    CGFloat titleLabelH = imageViewH;
    titleLabel.frame = CGRectMake(titleLabelX, titleLabelY, titleLabelW, titleLabelH);
}
@end
